/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.discovery.internal;

import java.util.Dictionary;
import java.util.Hashtable;

import nemos.remote.discovery.IRegistry;
import nemos.remote.util.RemoteUtil;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.EndpointListener;

/**
 * RegistryAgent : The endpoint registry agent.
 * 
 * <p>
 * This service listens for local endpoints and send them to the central
 * endpoint registry.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Component
public class RegistryAgent implements EndpointListener {

    /** The registry to update */
    @Reference(name = "registry", cardinality = ReferenceCardinality.MANDATORY_UNARY)
    private IRegistry mRegistry;
    /** The service registration */
    private ServiceRegistration<?> mRegistration;

    /**
     * Constructor
     */
    public RegistryAgent() {
        super();
    }

    /**
     * Start the component.
     * 
     * @param pContext
     *            The bundle context
     */
    @Activate
    public void start(final BundleContext pContext) {
        final Dictionary<String, Object> lProps = new Hashtable<String, Object>();
        lProps.put(ENDPOINT_LISTENER_SCOPE, RemoteUtil.getLocalEndpointFilter());
        mRegistration = pContext.registerService(EndpointListener.class, this, lProps);
    }

    /**
     * Stop the component.
     */
    @Deactivate
    public void stop() {
        mRegistration.unregister();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.osgi.service.remoteserviceadmin.EndpointListener#endpointAdded(org.osgi.service.remoteserviceadmin.EndpointDescription,
     *      java.lang.String)
     */
    @Override
    public void endpointAdded(final EndpointDescription pEndpoint,
            @SuppressWarnings("unused") final String pMatchedFilter) {
        mRegistry.register(pEndpoint.getProperties());
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.osgi.service.remoteserviceadmin.EndpointListener#endpointRemoved(org.osgi.service.remoteserviceadmin.EndpointDescription,
     *      java.lang.String)
     */
    @Override
    public void endpointRemoved(final EndpointDescription pEndpoint,
            @SuppressWarnings("unused") final String pMatchedFilter) {
        mRegistry.unregister(pEndpoint.getProperties());
    }

    /**
     * Bind the registry service.
     * 
     * @param pRegistry
     *            The registry to notify
     */
    public void bindRegistry(final IRegistry pRegistry) {
        mRegistry = pRegistry;
    }

    /**
     * Unbind the registry service.
     * 
     * @param pRegistry
     *            The notified registry
     */
    public void unbindRegistry(@SuppressWarnings("unused") final IRegistry pRegistry) {
        mRegistry = null;
    }
}
